package com.fjzx.androidframe.network;

import android.util.SparseArray;

/**
 * @author xiongyongcong
 */
public class RepoManager{
    private static RepoManager ins;
    private SparseArray<BaseRepo> repos = new SparseArray<>();

    public static RepoManager ins() {
        if (ins == null) {
            ins = new RepoManager();
        }
        return ins;
    }

    public <T extends BaseRepo> T getRepo(Class<T> clazz) {
        BaseRepo repo = repos.get(clazz.hashCode());
        if (repo == null) {
            try {
                repo = clazz.newInstance();
                repos.put(clazz.hashCode(), repo);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return (T) repo;
    }

    public <T extends BaseRepo> void clearRepo(T repo) {
        if (repos.get(repo.getClass().hashCode()) != null) {
            repos.delete(repo.getClass().hashCode());
        }
    }

    public void clearAllRepo() {
        if (repos != null) {
            repos.clear();
        }
    }
}
